package br.usp.poli.weblab.jsf;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;

import br.usp.poli.weblab.client.DataSetMetadata;
import br.usp.poli.weblab.client.Experiment;
import br.usp.poli.weblab.client.Instrument;
import br.usp.poli.weblab.client.Person;
import br.usp.poli.weblab.client.Variable;
import br.usp.poli.weblab.client.WebLab;
import br.usp.poli.ws.util.WebServiceUtil;

import com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl;

public class ExperimentHandler {

	private WebLab lab = WebServiceUtil.getWebLab();
	private Experiment experiment = new Experiment();
	private Date startDate = new Date();
	private Date endDate = new Date();
	
	private SelectItemGroup variables;
	
	{
		experiment.setDataSetMetadata(new DataSetMetadata());
	}

	public SelectItemGroup getVariables() {
		this.variables = new SelectItemGroup();
		List<Variable> listVariables = lab.listVariables();
		List<SelectItem> selectItems = new ArrayList<SelectItem>();
		for (Variable variable : listVariables) {
			Instrument instrument = variable.getInstrument();
			String label = variable.getName() + " @ " + instrument.getModel()
					+ " S/N: " + instrument.getSerial();
			selectItems.add(new SelectItem(variable, label));
		}
		SelectItem[] selectItemArray = selectItems.toArray(new SelectItem[0]);
		variables.setSelectItems(selectItemArray);
		return variables;
	}

	public List<Experiment> getList() {
		return lab.listExperiments();
	}

	public String submitExperiment() {
		System.out.println("submitExperiment ");
		GregorianCalendar gcStartDate = new GregorianCalendar();
		gcStartDate.setTime(startDate);
		experiment.getDataSetMetadata().setStartDate(new XMLGregorianCalendarImpl(gcStartDate));
		GregorianCalendar gcEndDate = new GregorianCalendar();
		gcEndDate.setTime(endDate);
		experiment.getDataSetMetadata().setStartDate(new XMLGregorianCalendarImpl(gcEndDate));
		display(experiment);
		lab.submitExperiment(this.experiment);
		return "success";
	}

	private void display(Experiment experiment) {
		System.out.println("Title: "+experiment.getDataSetMetadata().getTitle());
		System.out.println("Start: "+experiment.getDataSetMetadata().getStartDate());
		System.out.println("End: "+experiment.getDataSetMetadata().getEndDate());
		System.out.println("Variables");
		for (Variable v: experiment.getVariables()){
			System.out.println("Varible: "+v.getName()+" @ "+v.getInstrument());
		}
		System.out.println("Creator");
		for (Person p : experiment.getDataSetMetadata().getCreator()){
			System.out.println(p.getSurName());
		}
	}

	public Experiment getExperiment() {
		return experiment;
	}

	public void setExperiment(Experiment experiment) {
		this.experiment = experiment;
	}

	public void setVariables(SelectItemGroup variables) {
		SelectItem[] selectItems = variables.getSelectItems();
		for (SelectItem selectItem : selectItems) {
			System.out.println(selectItem.getLabel());
		}
		this.variables = variables;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

}
